package com.heg.api.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.heg.api.service.UserModuleService;
import com.heg.api.util.PublicMethodsUtils;
import com.heg.bk.dao.HibernateBaseDao;
import com.heg.bk.dao.MybatisBaseDao;
import com.heg.bk.entity.HegMobileUser;

/**
 * app用户模块逻辑层业务（实现层）
 * @author 邓绪招
 * @time 2016-04-18
 * @version 1.0.1
 */
@Service("userModuleServiceImpl")
public class UserModuleServiceImpl implements UserModuleService {
	
	@Resource
	private HibernateBaseDao hibernateDao;
	
	@Resource
	private MybatisBaseDao mybatisDao;
	
	public Boolean verification_isreg(String telnumber) {
		Object obj = mybatisDao.selectOne(telnumber, "mobileUserinfoSpace.verification_isreg");
		return obj != null ? false : true;
	}
	
	public Map<String, Object> regUser(String telnumber, String password, String code,
			String deviceVersion, String deviceType, String deviceKey,
			String deviceId, String userId, String channelId, String lat, String lng, String params1, String params2) {
		Integer device_type = 0;
		Double currLat = 0.00;
		Double currLng = 0.00;
		String icon = "";
		String backimage = "";
		String currTime = PublicMethodsUtils.getCurrTime();
		HegMobileUser hegMobileUser = new HegMobileUser();
		hegMobileUser.setTelnumber(telnumber);
		hegMobileUser.setPassword(password);
		hegMobileUser.setBackImge(backimage);
		hegMobileUser.setUicon(icon);
		hegMobileUser.setDeviceversion(deviceVersion);
		hegMobileUser.setDevicetype(device_type);
		hegMobileUser.setDevicekey(deviceKey);
		hegMobileUser.setDeviceId(deviceId);
		hegMobileUser.setLat(currLat);
		hegMobileUser.setLng(currLng);
		hegMobileUser.setUserId(userId);
		hegMobileUser.setCreatetime(currTime);
		hegMobileUser.setUpdatetime(currTime);
		hegMobileUser.setChannelId(channelId);
		hegMobileUser.setIntegral(0);
		hegMobileUser.setUsertype(0);
		hegMobileUser.setGrade(0);
		hegMobileUser.setIsdel(0);
		Map<String, Object> retMaps = new HashMap<String, Object>();
		Integer rows = mybatisDao.insert(hegMobileUser, "mobileUserinfoSpace.insert_mobileUser_toObj");
		if(rows > 0){
			retMaps.put("uid", hegMobileUser.getUid());
			return retMaps;
		} else {
			return null;
		}
	}
	
	public HegMobileUser appUser_firstLogin(String telNumber, String password,
			String lat, String lng, String userId, String channelId) {
		Double currLat = Double.valueOf(lat);
		Double currLng = Double.valueOf(lng);
		HegMobileUser paramsUser = new HegMobileUser(telNumber, password);
		List lst = mybatisDao.selectList(paramsUser, "mobileUserinfoSpace.appUser_firstLogin");
		if(lst != null && lst.size() > 0){
			HegMobileUser retUser = (HegMobileUser) lst.get(0);
			Integer uid = retUser.getUid();
			HegMobileUser updUser = new HegMobileUser();
			updUser.setUid(uid);
			updUser.setLat(currLat);
			updUser.setLng(currLng);
			updUser.setUserId(userId);
			updUser.setChannelId(channelId);
			updUser.setUpdatetime(PublicMethodsUtils.getCurrTime());
			mybatisDao.update(updUser, "mobileUserinfoSpace.updateByUid_toLangId");
			return retUser;
		} else {
			return null;
		}
	}

	public HegMobileUser appUser_non_firstLogin(String tokenCode, String lat,
			String lng, String userId, String channelId) {
		Double currLat = Double.valueOf(lat);
		Double currLng = Double.valueOf(lng);
		Object obj = mybatisDao.selectList(tokenCode, "mobileUserinfoSpace.appUser_non_firstLogin");
		if(obj != null){
			HegMobileUser retUser = (HegMobileUser) obj;
			Integer uid = retUser.getUid();
			HegMobileUser updUser = new HegMobileUser();
			updUser.setUid(uid);
			updUser.setLat(currLat);
			updUser.setLng(currLng);
			updUser.setUserId(userId);
			updUser.setChannelId(channelId);
			updUser.setUpdatetime(PublicMethodsUtils.getCurrTime());
			mybatisDao.update(updUser, "mobileUserinfoSpace.updateByUid_toLangId");
			return retUser;
		} else {
			return null;
		}
	}

	public List<Object[]> get_UserByUid(String uid) {
		return null;
	}

}
